package com.neudu.glsb.controller;

import com.github.pagehelper.PageInfo;
import com.neudu.glsb.commons.JSONReturn;
import com.neudu.glsb.pojo.AbnormalOrder;
import com.neudu.glsb.service.AbnormalOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.util.ObjectUtils;

import java.util.Map;

/**
 * 异常订单Controller（标准分层：调用Service层）
 */
@RestController
public class AbnormalOrderController {

    // 注入Service接口（而非直接注入Mapper）
    @Autowired
    private AbnormalOrderService abnormalOrderService;

    @Autowired
    private JSONReturn jsonReturn;

    /**
     * 员工上传异常订单
     */
    @PostMapping("/upload_abnormal_order")
    public String uploadAbnormalOrder(@RequestBody AbnormalOrder abnormalOrder) {
        try {
            // 基础参数校验（Controller层负责参数合法性校验）
            if (ObjectUtils.isEmpty(abnormalOrder.getOrderNo())
                    || ObjectUtils.isEmpty(abnormalOrder.getOrderDate())
                    || ObjectUtils.isEmpty(abnormalOrder.getAbnormalType())
                    || ObjectUtils.isEmpty(abnormalOrder.getAbnormalDesc())) {
                return jsonReturn.returnError("订单号、订单日期、异常类型、异常描述为必填项");
            }

            // 调用Service层处理业务
            boolean success = abnormalOrderService.uploadAbnormalOrder(abnormalOrder);
            return success ? jsonReturn.returnSuccess("异常订单上传成功") : jsonReturn.returnError("上传失败");
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError("上传失败：" + e.getMessage());
        }
    }

    /**
     * 员工查询自己的异常订单
     */
    @PostMapping("/get_abnormal_order_list")
    public String getAbnormalOrderList(@RequestBody Map<String, Object> map) {
        try {
            // 分页参数校验
            if (map.get("pageNum") == null || map.get("pageSize") == null) {
                return jsonReturn.returnError("分页参数pageNum和pageSize不能为空");
            }

            // 调用Service层查询
            PageInfo<AbnormalOrder> pageInfo = abnormalOrderService.getAbnormalOrderListByCreateBy(map);
            return jsonReturn.returnSuccess(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError("查询失败：" + e.getMessage());
        }
    }

    /**
     * 管理员查询所有异常订单
     */
    @PostMapping("/admin/get_abnormal_order_list")
    public String adminGetAbnormalOrderList(@RequestBody Map<String, Object> map) {
        try {
            // 分页参数校验
            if (map.get("pageNum") == null || map.get("pageSize") == null) {
                return jsonReturn.returnError("分页参数pageNum和pageSize不能为空");
            }

            // 日期格式预处理（Controller层处理参数格式）
            if (map.get("startDate") != null) {
                String start = map.get("startDate").toString().trim();
                if (!start.isEmpty() && start.length() == 10) {
                    map.put("startDate", start + " 00:00:00");
                }
            }
            if (map.get("endDate") != null) {
                String end = map.get("endDate").toString().trim();
                if (!end.isEmpty() && end.length() == 10) {
                    map.put("endDate", end + " 23:59:59");
                }
            }

            // 调用Service层查询
            PageInfo<AbnormalOrder> pageInfo = abnormalOrderService.getAllAbnormalOrderList(map);
            return jsonReturn.returnSuccess(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError("查询失败：" + e.getMessage());
        }
    }

    /**
     * 管理员处理异常订单
     */
    @PostMapping("/admin/handle_abnormal_order")
    public String handleAbnormalOrder(@RequestBody Map<String, Object> map) {
        try {
            // 提取并校验参数
            Integer id = (Integer) map.get("id");
            String handleStatus = (String) map.get("handleStatus");
            String handleBy = (String) map.get("handleBy");
            String handleRemark = (String) map.get("handleRemark");

            if (ObjectUtils.isEmpty(id) || ObjectUtils.isEmpty(handleStatus)
                    || ObjectUtils.isEmpty(handleBy) || ObjectUtils.isEmpty(handleRemark)) {
                return jsonReturn.returnError("ID、处理状态、处理人、处理备注为必填项");
            }

            // 调用Service层处理
            boolean success = abnormalOrderService.handleAbnormalOrder(id, handleStatus, handleBy, handleRemark);
            return success ? jsonReturn.returnSuccess("处理成功") : jsonReturn.returnError("处理失败");
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError("处理失败：" + e.getMessage());
        }
    }
}